<?php
class Category_Model_Label extends WOW_Model_Acl_Abstract
{
    public function getResourceId()
    {
        return 'Label';
    }

    public function getLabelById($id)
    {
        $id = (int) $id;
        return $this->getResource('Label')->getLabelById($id);
    }

    public function getAllLabels()
    {
        return $this->getResource('Label')->getAllLabels();
    }

    public function getLabelByParentCategoryId($catid)
    {
        return $this->getResource('Label')->getLabelByParentCategoryId($catid);
    }

    public function getLabelByParentSubcatId($subcatid)
    {
        return $this->getResource('Label')->getLabelByParentSubcatId($subcatid);
    }

    public function getParentSubCategory($labelid=null)
    {
        return $this->getResource('Label')->getParentSubCategory($labelid);
    }

    public function getParentCategory($labelid=null)
    {
        return $this->getResource('Label')->getParentCategory($labelid);
    }

    public function getAllLabelArray()
    {
        $all_label  = $this->getResource('Label')->getAllLabels();
        $ret_ar = array(' ' => 'Select Subcategory Label');
        foreach($all_label as $label)
        {
            $ret_ar[$label->labelid] = $label->LabelName;
        }

        return $ret_ar;
    }

    public function deleteLabel($label)
    {
        if ($label instanceof Category_Resource_Label_Item_Interface) {
            $labelid = (int) $label->labelid;
        } else {
           $labelid = (int) $label;
        }

        $label = $this->getLabelById($labelid);

        if (null !== $label) {
            $label->delete();
            return true;
        }
        else
             throw new WOW_Exception('Unknown Sub-category Label');

        return false;
    }

    public function saveLabel($post)
    {
        $form = $this->getForm('LabelsLabelform');
        return $this->_save($form, $post);
    }

    protected function _save(Zend_Form $form, array $info, $defaults=array())
    {
       if (!$form->isValid($info))
       {
            // echo '<br> returning false formnot valid';
            return false;
       }
        $data = $form->getValues();
        
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }

        if($data['labelid'] !='')
        {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $label = ($data['labelid'] !='') ? $this->getResource('Label')->getLabelById($data['labelid']) : null;

        return $this->getResource('Label')->saveRow($data, $label);
    }

    public function getLabelDataArray($catid, $subcatid)
    {
        $all_label  = $this->getResource('Label')->getLabelData($catid, $subcatid);
        
        $ret_ar = array('-1' => 'Select Subcategory Label');
        
        foreach($all_label as $label)
        {
            $ret_ar[$label->labelid] = $label->LabelName;
        }
        
        return $ret_ar;
    }

     #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveCategory'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }
}
?>